﻿using STMS.BLL;
using STMS.Models.DModels;
using STMS.WSTMSystem.FModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem.Store
{
    public partial class FrmStoreList : Form
    {
        public FrmStoreList()
        {
            InitializeComponent();
        }

        StoreBLL storeBLL = new StoreBLL();
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmStoreList1_Load(object sender, EventArgs e)
        {
            chkShowDel.Checked = false;
            FindStoreList();
        }

        /// <summary>
        /// 查询仓库列表
        /// </summary>
        private void FindStoreList()
        {
            string keywords = txtKeyWords.Text.Trim();
            bool isShow = chkShowDel.Checked;
            List<StoreInfo> list = storeBLL.GetStoreList(keywords, isShow);
            if (list.Count > 0)
            {
                dgvStoreList.AutoGenerateColumns = false;
                dgvStoreList.DataSource = list;
                SetBtnsEnable(true);
                dgvStoreList.ShowDgvBtnCols(isShow);
                dgvStoreList.ShowDgvCol("colAddRegion", !isShow);
            }
            else
            {
                dgvStoreList.DataSource = null;
                SetBtnsEnable(false);
            }
        }

        private void SetBtnsEnable(bool bl)
        {
            btnFind.Enabled = bl;
            btnDelete.Enabled = bl;
            dgvStoreList.Visible = bl;
        }


        /// <summary>
        /// 添加仓库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            ShowStoreInfoPage(1, 0);
        }

        private void dgvStoreList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                DataGridViewCell curCell = dgvStoreList.Rows[e.RowIndex].Cells[e.ColumnIndex];
                StoreInfo store = dgvStoreList.Rows[e.RowIndex].DataBoundItem as StoreInfo;
                switch (curCell.FormattedValue.ToString())
                {
                    case "添加分区":
                        ShowStoreRegionPage(store.StoreId);
                        break;
                    case "修改":
                        ShowStoreInfoPage(2, store.StoreId);
                        break;
                    case "删除":
                        DeleteStore(store, 1);
                        break;
                    case "恢复":
                        DeleteStore(store, 0);
                        break;
                    case "移除":
                        DeleteStore(store, 2);
                        break;
                    default: break;
                }
            }
        }

        /// <summary>
        /// 打开添加仓库分区页面
        /// </summary>
        /// <param name="storeId"></param>
        private void ShowStoreRegionPage(int storeId)
        {
            FrmStoreRegionInfo fSRegionInfo = new FrmStoreRegionInfo();
            fSRegionInfo.Tag = new FInfoData()
            {
                ActType = 3,
                FId = storeId
            };
            fSRegionInfo.ReloadStoreList += FindStoreList;
            fSRegionInfo.ShowDialog();
        }

        /// <summary>
        /// 打开仓库信息页
        /// </summary>
        /// <param name="actType"></param>
        /// <param name="storeId"></param>
        private void ShowStoreInfoPage(int actType, int storeId)
        {
            FrmStoreInfo fStore = new FrmStoreInfo();
            fStore.Tag = new FInfoData()
            {
                ActType = actType,
                FId = storeId
            };
            fStore.ReloadStoreList += FindStoreList;
            fStore.ShowDialog();
        }

        /// <summary>
        /// 删除/恢复/真删除 仓库信息
        /// </summary>
        /// <param name="store"></param>
        /// <param name="delType"></param>
        private void DeleteStore(StoreInfo store, int delType)
        {
            string delTypeName = "";
            string infoName = "仓库";
            switch (delType)
            {
                case 1: delTypeName = "删除"; break;
                case 0: delTypeName = "恢复"; break;
                case 2: delTypeName = "移除"; break;
            }
            string msgTitle = $"{delTypeName}{infoName}";
            if (MsgBoxHelper.MsgBoxConfirm(msgTitle, $"您确定要{delTypeName}该{infoName}信息吗？") == DialogResult.Yes)
            {
                //删除
                bool bl = false;
                switch (delType)
                {
                    case 1:
                        int re = storeBLL.LogicDeleteStore(store.StoreId);
                        if (re == 2)
                        {
                            MsgBoxHelper.MsgErrorShow(msgTitle, $"{infoName}:{store.StoreName} 已添加了分区，不能删除！");
                            return;
                        }
                        else if (re == 1)
                            bl = true;
                        else
                            bl = false;
                        break;
                    case 0:
                        bl = storeBLL.RecoverStore(store.StoreId);
                        break;
                    case 2:
                        bl = storeBLL.DeleteStore(store.StoreId);
                        break;
                }
                string sucText = bl ? "成功" : "失败";
                string msg = $"{infoName}：{store.StoreName} {delTypeName}{sucText}！";
                if (bl)
                {
                    MsgBoxHelper.MsgBoxShow(msgTitle, msg);
                    FindStoreList();
                }
                else
                {
                    MsgBoxHelper.MsgErrorShow(msgTitle, msg);
                    return;
                }
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            FindStoreList();
        }

        private void ChkShowDel_CheckedChanged(object sender, EventArgs e)
        {
            FindStoreList();
        }

        /// <summary>
        /// 删除  可删多条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            List<int> storeIds = new List<int>();
            foreach (DataGridViewRow row in dgvStoreList.Rows)
            {
                StoreInfo store = row.DataBoundItem as StoreInfo;
                DataGridViewCheckBoxCell chkCell = row.Cells["colChk"] as DataGridViewCheckBoxCell;
                if (chkCell.FormattedValue.ToString() == "True")
                {
                    storeIds.Add(store.StoreId);
                }
            }
            string msgTitle = "删除仓库";
            if (storeIds.Count > 0)
            {
                if (MsgBoxHelper.MsgBoxConfirm(msgTitle, "你确定要删除选择的仓库信息吗？") == DialogResult.Yes)
                {
                    string reDel = storeBLL.LogicDeleteStore(storeIds);
                    if (reDel != "" && reDel != "0")
                    {
                        MsgBoxHelper.MsgErrorShow(msgTitle, $"选择的仓库中存在分区，它们的编号是：{reDel}");
                        return;
                    }
                    else if (reDel == "")
                    {
                        MsgBoxHelper.MsgBoxShow(msgTitle, $"选择的仓库删除成功！");
                        FindStoreList();
                    }
                    else
                    {
                        MsgBoxHelper.MsgErrorShow(msgTitle, $"选择的仓库删除失败！");
                        return;
                    }
                }
            }
            else
            {
                MsgBoxHelper.MsgErrorShow("删除仓库", "请选择要删除的仓库信息！");
                return;
            }
        }

        private void btnStoreRegionList_Click(object sender, EventArgs e)
        {
            Store.FrmStoreRegionList fStoreRegionList = new Store.FrmStoreRegionList();
            Panel panel = this.Parent as Panel;
            panel.AddPanelPage(fStoreRegionList);
            
   
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            FrmStoreList1_Load(this, e);
        }
    }
}
